<template>
  <div class="app-container">
    <el-button @click="dialogFormVisible = true">添加二级分类</el-button>
    <br/><br/><br/>

    <el-select @change="setSC" v-model="selectFir" placeholder="请选择一级分类">
      <el-option
        v-for="(item,index) in firCla"
        :key="item.id"
        :label="item.name"
        :value="index"
      />
    </el-select>

    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="addForm" label-width="120px">
        <el-form-item label="所属父标签">
          <el-select v-model="addForm.pid" placeholder="选择所属分类">
            <el-option v-for="fc in firCla" :key="fc.id" :label="fc.name" :value="fc.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="输入标签名">
          <el-input v-model="addForm.name"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSC">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="updateVisible">
      <el-form ref="updateForm" :model="updateForm" label-width="120px">
        <el-form-item label="所属父标签">
          <el-select v-model="updateForm.pid" placeholder="选择所属父标签">
            <el-option v-for="fc in firCla" :key="fc.id" :label="fc.name" :value="fc.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="输入标签名">
          <el-input v-model="updateForm.name"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updSC">确 定</el-button>
      </div>
    </el-dialog>

    <el-table v-loading="loading" :data="secCla" stripe style="width: 100%; margin-top: 30px">
      <el-table-column prop="id" label="#" width="200"/>
      <el-table-column prop="name" label="标签名" width="250">
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="danger" @click="delSC(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { getFirCla, addSecCla, delSecCla, updSecCla } from '@/api/classification'

export default {
  data() {
    return {
      firCla: [],
      selectFir: 0,
      secCla: [],
      addForm: {
        pid: '',
        name: ''
      },
      updateForm: {
        id: '',
        pid: '',
        name: ''
      },
      updateVisible: false,
      dialogFormVisible: false,
      loading: false
    }
  },
  mounted() {
    this.getFC()
  },
  methods: {
    getFC() {
      this.loading = true
      getFirCla().then(res => {
        this.firCla = res.data
        this.setSC()
        this.loading = false
      })
    },

    setSC() {
      this.secCla = this.firCla[this.selectFir].sec
    },

    addSC() {
      addSecCla(this.addForm).then(res => {
        this.getFC()
        this.$message('添加成功!')
        this.dialogFormVisible = false
      })
    },

    delSC(id) {
      delSecCla(id).then(res => {
        this.getFC()
        this.$message('删除成功!')
      })
    },

    handleUpdate($obj) {
      this.updateForm.id = $obj.id
      this.updateForm.pid = $obj.pid
      this.updateForm.name = $obj.name
      this.updateVisible = true
    },

    updSC() {
      updSecCla(this.updateForm).then(res => {
        this.getFC()
        this.$message('修改成功!')
        this.updateVisible = false
      })
    }
  }
}
</script>
